package Flights;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="passengers")
@SessionScoped
public class Passengers {

	private HashMap<String, Passenger> passengersMap = new HashMap<String, Passenger>();
	private ArrayList<Passenger> passengers = new ArrayList<Passenger>();
	private Passenger passenger;
	
	public Passengers(String path) {
		BufferedReader in = null;
		try {
			File file = new File(path);
			System.out.println(file.getCanonicalPath());
			in = new BufferedReader(new FileReader(file));
			readPassengers(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if ( in != null ) {
				try {
					in.close();
				}
				catch (Exception e) { }
			}
		}
	}

	private synchronized void readPassengers(BufferedReader in) {
		String line, JMBG = "", ime = "", prezime = "";
		StringTokenizer st;
		try {
			while ((line = in.readLine()) != null) {
				line = line.trim();
				if (line.equals("") || line.indexOf('#') == 0)
					continue;
				st = new StringTokenizer(line, ";");
				while (st.hasMoreTokens()) {
					JMBG = st.nextToken().trim();
					ime = st.nextToken().trim();
					prezime = st.nextToken().trim();
				}
				Passenger p = new Passenger(JMBG, ime, prezime);
				passengers.add(p);
				passengersMap.put(JMBG, p);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public ArrayList<Passenger> getPassengers() {
		return passengers;
	}
	public void setPassengers(ArrayList<Passenger> passengers) {
		this.passengers = passengers;
	}
	public Passenger getPassenger() {
		return passenger;
	}
	public void setPassenger(Passenger passenger) {
		this.passenger = passenger;
	}
	public HashMap<String, Passenger> getPassengersMap() {
		return passengersMap;
	}
	
}
